package com.qfedu.provider.controller;

import com.alipay.api.domain.Video;
import com.qfedu.provider.entity.DbVideo;
import com.qfedu.params.AddVideo;
import com.qfedu.provider.service.DbVideoService;
import com.qfedu.provider.util.MyMinioUtil;
import com.qfedu.utils.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * @author Administrator
 * @date 2024/11/23 上午11:21
 */
@RestController
@RequestMapping("/server/video/")
@Tag(name = "视频管理")
@RequiredArgsConstructor
public class VideoController {
    private final DbVideoService dbVideoService;
    @Operation(summary = "根据用户id查询视频")
    @GetMapping("findByUserId")
    public List<DbVideo> findByUserId(Integer userId) {
        return dbVideoService.findByUserId(userId);
    }

    @Operation(summary = "查询所有视频")
    @GetMapping("findAll")
    public List<DbVideo> findAll() {
        return dbVideoService.findAll();
    }



    @Operation(summary = "根据类型查询视频")
    @GetMapping("findByType")
    public R findByType(Integer typeId) {
        return dbVideoService.findByTypeId(typeId);
    }


    @Operation(summary = "根据视频id删除视频")
    @DeleteMapping("deleteById")
    public R deleteById(Integer videoId) {
        return dbVideoService.deleteById(videoId);
    }
    @PostMapping(value = "/uploadVideo")
    @Operation(summary = "发布视频")
    public R upload(@RequestPart MultipartFile video, @RequestPart MultipartFile displayImage, String uid, String description, Integer typeId, Integer productTypeId) throws IOException {
        System.out.println("用户发布的文字信息"+description);
        String imageName = System.currentTimeMillis() + "_" + displayImage.getOriginalFilename();
        byte[] dataImage = displayImage.getBytes();
        String urlImage = MyMinioUtil.upload(imageName, dataImage);
        String videoName = System.currentTimeMillis() + "_" + video.getOriginalFilename();
        byte[] dataVideo = video.getBytes();
        String urlVideo = MyMinioUtil.upload(videoName, dataVideo);
        AddVideo addVideo = new AddVideo(description,urlVideo,typeId,productTypeId,urlImage);
        return dbVideoService.publish(addVideo,Integer.parseInt(uid));
    }
}